<?php 

error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE );

ini_set('max_execution_time', 43200);

require_once("../config/constant.php");
require_once(DAEMONE_HOME."/lib/lib_html_reader.php");
require_once(DAEMONE_HOME."/lib/lib_rss_database.php");
require_once(DAEMONE_HOME."/lib/lib_database.php");

while(true)
{
	$status = get_rss_grab_full_article_status();
	echo 'checking process status', $status, "<br/>\r\n";
	if( $status == 'STOP')
		break;
	else 
		sleep(30);
}
execute_query("update config set signal='RUNNING', status='RUNNING' where process_key='rss_grab_full_article'");
echo 'starting article grabing process ... ', "<br/>\r\n";
$counter = 0;
$counter_limit = $config['rss']['item.process.total.limit'];
$break_process = FALSE;
while(true)
{
	if($counter > $counter_limit || $break_process == TRUE)
			break;
		
	// get ungrabbed rss items
	$result = get_rss_feed_items('WHERE grabbed_status = 1 ', ' ORDER BY rss_feed_grabed_on DESC ', 'LIMIT '.$config['rss']['item.process.sub.limit']);
	if($result === FALSE || mysql_num_rows($result) == 0) 
	{
		echo 'RSS Feed Item : No more records to grab full article.';
		$break_process == TRUE;
		break;
	}

	// iterate rss feed items
	while ($row = mysql_fetch_assoc($result)) 
	{
	
		if(get_rss_grab_full_article_signal() == 'STOP' || $counter > $counter_limit)
		{
			$break_process = TRUE;
			break;
		}
			
		$counter = $counter + 1;
		
		$rss_feed_url = $row['link'];
		$rss_feed_item_id = $row['item_id'];
		try 
		{
			$response = html_retrieve_ezin_article($rss_feed_url);
		}
		catch(Exception $e)
		{
			echo $e;
			sleep(30);
		}
		if ($response['http_code'] == 403)
		{
			$break_process = TRUE;
			break;
		}
		
		$date = new DateTime(); 
		$date->setTimezone(new DateTimeZone('Asia/Karachi'));
	
		if ($response['http_code'] == 200 && $response['status'] == 'SUCCESS')
		{
			$data_update['grabbed_status'] = 2; // article grabbed success
			$data_update['grab_http_code'] = $response['http_code'];	
			$data_update['grab_http_status'] = $response['status'];	
			$data_update['article_original_url'] = $response['article_original_url'];
			$data_update['html_meta_keywords'] = $response['html_meta_keywords'];
			$data_update['html_meta_description'] = $response['html_meta_description'];
			$data_update['html_article'] = $response['html_article'];
			$data_update['html_author_name'] = $response['html_author_name'];
			$data_update['html_author_url'] = $response['html_author_url'];
			$data_update['html_author_membership_type'] = $response['html_author_membership_type'];
			$data_update['html_author_country'] = $response['html_author_country'];
			$data_update['article_grabed_on'] = date_format($date, "Y-m-d H:i:s");
			$data_update['html_body_length'] = strlen($response['html_body']);
			update_rss_feed_item_related_article($rss_feed_item_id, $response['html_similiar_articles']);
		}
		else 
		{
			
			$data_update['grabbed_status'] = -1;  // article grabbed fail
			$data_update['grab_http_code'] = $response['http_code'];
			$data_update['grab_http_status'] = $response['status'];	
			$data_update['html_body'] = $response['html_body'];
			$data_update['html_body_length'] = strlen($response['html_body']);
			$data_update['article_grabed_on'] = date_format($date, "Y-m-d H:i:s");
		}
		// echo "item_id = ${rss_feed_item_id},  grab_http_code = ${data_update['grab_http_code']} <br/>\r\n";
		update_rss_feed_item($rss_feed_item_id, $data_update);
	}
}
execute_query("update config set status='STOP' where process_key='rss_grab_full_article'");
echo "total articles grabbed $counter";
exit;
?>
